package com.jinyang.jetpackdemo.activity

import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.jinyang.jetpackdemo.R
import com.jinyang.jetpackdemo.util.LjyLogUtil

class GlideDemoActivity : AppCompatActivity() {
    private lateinit var imageView1: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_glide_demo)
        LjyLogUtil.d("Activity.onCreate:${savedInstanceState}")
        test()
    }

    private fun test() {
//        val imgUrl = ""
        val imgUrl = "https://pic3.zhimg.com/v2-dc32dcddfd7e78e56cc4b6f689a24979_xl.jpg"
        imageView1 = findViewById(R.id.iv_glide_1)

        Glide.with(this)
            .asBitmap()
            .load(imgUrl)
            .into(imageView1)
        val imageView2 = findViewById<ImageView>(R.id.iv_glide_2)
        Glide.with(this)//上下文对象Context
            .load(imgUrl)//图片地址
            .placeholder(R.mipmap.ic_launcher)//加载前的占位图
            .thumbnail(
                Glide.with(this).load(imgUrl).override(20)
            )//设置缩略图
            .thumbnail(0.25f)//压缩原图为缩略图
            .error(R.mipmap.ic_launcher)//加载失败的占位图
            .override(150, 100)//指定图片尺寸(一般不用设置，Glide会自动根据ImageView的大小来决定图片的大小)
            .skipMemoryCache(true)//跳过内存缓存
            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)//根据图片资源智能地选择使用哪一种缓存策略（默认选项）
            .diskCacheStrategy(DiskCacheStrategy.ALL)//既缓存原始图片，也缓存转换过的图片
            .diskCacheStrategy(DiskCacheStrategy.DATA)//只缓存原始图片
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//只缓存转换过的图片
            .diskCacheStrategy(DiskCacheStrategy.NONE)//禁用磁盘缓存
            .priority(Priority.HIGH)//优先级
            .into(imageView2)

        val options = RequestOptions()
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
        Glide.with(this)
            .load(imgUrl)
            .apply(options)
            .into(imageView1)
        Glide.with(this)
            .load(imgUrl)
            .apply(options)
            .into(imageView2)
    }

    fun onBtnCLick(view: android.view.View) {
        when (view.id) {
            R.id.btn_clear1 -> Glide.with(this).clear(imageView1)
        }
    }

    override fun onStart() {
        super.onStart()
        LjyLogUtil.d("Activity.onStart")
    }

    override fun onRestart() {
        super.onRestart()
        LjyLogUtil.d("Activity.onRestart")
    }

    override fun onResume() {
        super.onResume()
        LjyLogUtil.d("Activity.onResume")
    }

    override fun onPause() {
        super.onPause()
        LjyLogUtil.d("Activity.onPause")
    }

    override fun onStop() {
        super.onStop()
        LjyLogUtil.d("Activity.onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        LjyLogUtil.d("Activity.onDestroy")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        LjyLogUtil.d("Activity.onSaveInstanceState")
        outState.putString("key1","value1")
        outState.putString("key2","value2")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        LjyLogUtil.d("Activity.onRestoreInstanceState:${savedInstanceState}")
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        LjyLogUtil.d("Activity.onConfigurationChanged:${newConfig}")
    }

}